package com.joe.blockview.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;

import com.joe.blockview.R;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class BlockVoiceView extends BlockView {
    public BlockVoiceView(Context context) {
        super(context);
    }

    public BlockVoiceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BlockVoiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mBlockViewSize.x = selfWidth;
        mBlockViewSize.y = selfHeight;
        setMeasuredDimension(mBlockViewSize.x, mBlockViewSize.y);
    }


    @Override
    public void init() {
        super.init();
        connection.setType(1);
        selfHeight = 150;
        selfWidth = 345;

        setCommand("Voice");
        setName("Voice");
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.rgb(89, 162, 216));

        //添加路径
        Path path1 = new Path();
        path1.moveTo(10, 0);
        path1.lineTo(40, 0);
        path1.lineTo(60, 20);
        path1.lineTo(80, 20);
        path1.lineTo(100, 0);
        path1.lineTo(250, 0);
        path1.addArc(185, 0, 345, 130, -90, 180);
        path1.lineTo(250, 130);
        path1.lineTo(100, 130);
        path1.lineTo(80, 150);
        path1.lineTo(60, 150);
        path1.lineTo(40, 130);
        path1.lineTo(10, 130);
        path1.arcTo(0, 110, 20, 130, 90, 90, false);
        path1.lineTo(0, 30);
        path1.arcTo(0, 0, 20, 20, 180, 90, false);

        //填充内部颜色
        canvas.drawPath(path1, paint);


        //绘制边界
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.rgb(35, 111, 159));
        paint.setStrokeWidth(2);
        canvas.drawPath(path1, paint);

//        paint.setColor(Color.WHITE);
//        paint.setTextSize(40);
//        paint.setTypeface(Typeface.DEFAULT_BOLD);
//        paint.setStyle(Paint.Style.FILL);
//        canvas.drawText("if",100,80,paint);

//        Path path = new Path();
//        path.moveTo(180,65);
//        path.lineTo(210,25);
//        path.lineTo(370,25);
//        path.lineTo(400,65);
//        path.lineTo(370,105);
//        path.lineTo(210,105);
//        path.lineTo(180,65);
//        paint.setColor(Color.rgb(60,92,169));
//        canvas.drawPath(path,paint);


        //添加图标
        Drawable d = getResources().getDrawable(R.drawable.block_voice_ic1);
        Bitmap bp = drawableToBitmap(d);
        canvas.drawBitmap(bp, 150f, 45f, paint);

        Drawable d2 = getResources().getDrawable(R.drawable.block_voice_ic2);
        Bitmap bp2 = drawableToBitmap2(d2);
        canvas.drawBitmap(bp2, 240f, 30f, paint);

    }

    //drawable转bitmap
    private Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(45, 45, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, 45, 45);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }

    //drawable转bitmap
    private Bitmap drawableToBitmap2(Drawable drawable) {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(80, 80, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, 80, 80);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}
